package com.example.ems.servlet;

import com.example.ems.bean.Employee;
import com.example.ems.bean.Salary;
import com.example.ems.dao.EmployeeDAO;
import com.example.ems.dao.SalaryDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/SalaryServlet")
public class SalaryServlet extends HttpServlet {
    private SalaryDAO salaryDAO;
    private EmployeeDAO employeeDAO;

    public void init() {
        salaryDAO = new SalaryDAO();
        employeeDAO = new EmployeeDAO();
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");


        String idStr = request.getParameter("id");
        int empId = Integer.parseInt(request.getParameter("empId"));
        int year = Integer.parseInt(request.getParameter("salaryYear"));
        int month = Integer.parseInt(request.getParameter("salaryMonth"));
        double amount = Double.parseDouble(request.getParameter("amount"));
        String status = request.getParameter("status");

        Salary salary = new Salary();
        if (idStr != null && !idStr.isEmpty()) {
            salary.setId(Integer.parseInt(idStr));
        }
        salary.setEmpId(empId);
        salary.setSalaryYear(year);
        salary.setSalaryMonth(month);
        salary.setAmount(amount);
        salary.setStatus(status);

        if ("add".equals(action)) {
            salaryDAO.addSalary(salary);
        } else if ("update".equals(action)) {
            salaryDAO.updateSalary(salary);
        }
        response.sendRedirect(request.getContextPath() + "/SalaryServlet?action=list");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        String searchEmpName = request.getParameter("searchEmpName");
        request.setAttribute("searchEmpNameVal", searchEmpName);

        if (action == null) action = "list";


        List<Employee> listEmployeeForSalaryForm = employeeDAO.getAllEmployees();
        request.setAttribute("listEmployeeForSalary", listEmployeeForSalaryForm);

        switch (action) {
            case "new":
            case "edit":
                if("new".equals(action)){
                    request.getRequestDispatcher("/salary_form.jsp").forward(request, response);
                } else { // edit
                    int id = Integer.parseInt(request.getParameter("id"));
                    Salary existingSalary = salaryDAO.getSalaryById(id);
                    request.setAttribute("salary", existingSalary);
                    request.getRequestDispatcher("/salary_form.jsp").forward(request, response);
                }
                break;
            case "delete":
                int deleteId = Integer.parseInt(request.getParameter("id"));
                salaryDAO.deleteSalary(deleteId);
                response.sendRedirect(request.getContextPath() + "/SalaryServlet?action=list" + (searchEmpName != null ? "&searchEmpName=" + java.net.URLEncoder.encode(searchEmpName, "UTF-8") : ""));
                break;
            case "list":
            default:
                List<Salary> listSalary;
                if (searchEmpName != null && !searchEmpName.trim().isEmpty()) {
                    listSalary = salaryDAO.getSalariesByEmployeeName(searchEmpName.trim());
                } else {
                    listSalary = salaryDAO.getAllSalaries();
                }
                request.setAttribute("listSalary", listSalary);
                request.getRequestDispatcher("/salary_list.jsp").forward(request, response);
                break;
        }
    }

}
